package com.molichuxing.framework.exception;

import java.net.BindException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.molichuxing.framework.result.ResponseCodeEnum;
import com.molichuxing.framework.result.ResponseResult;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

	@ExceptionHandler(value = Exception.class)

	public ResponseResult<String> exceptionHandler(HttpServletRequest request, Exception e) {
		e.printStackTrace();
		ResponseResult<String> result = new ResponseResult<>();

		if (e instanceof GlobalException) {
			GlobalException ex = (GlobalException) e;
			return result.error(ex.getCodeEnum());
		} else if (e instanceof BindException) {
			return result.error(ResponseCodeEnum.PARMS_ERROR);
		}

		return result.error(ResponseCodeEnum.UNKNOWN_ERROR);
	}
}
